<template>
  <div>
    <div class="docker">
      <!-- container -->
      <div div="container">
        <!-- nav -->
        <div class="nav">
          <a v-if="$auth.front_username == ''">
            <a style="color:#DE4323;margin-left: 150px;">
              <router-link to="/login">亲，请登录</router-link>
            </a>
            <a class="div">免费注册</a>
          </a>
          <a v-else>
            <a style="color:#DE4323;margin-left: 150px;">
              <router-link to="/login">用户: {{ $auth.front_username }}</router-link>
            </a>
            <a class="div" @click="Logout">登出</a>
          </a>
          <a style="margin-left: 400px;"></a>
          <a class="push"></a>
          <a class="push">
            <router-link to="/">科购首页</router-link>
          </a>
          <a class="push">
            <router-link to="/GoodsCar"><img src="../assets/car.png">&nbsp购物车</router-link>
          </a>
          <a class="push">
            <router-link to="/orderList">订单详情</router-link>
          </a>
          <a class="push">
            <router-link to="/Recharge">充值页面</router-link>
          </a>
          <a class="push">
            <router-link to="/feedback">用户反馈</router-link>
          </a>
          <a class="push"></a>
        </div>
          <a class="push"></a>
        </div>
        <!-- main -->
        <div class="main">
          <div class="logo">
            <h1>科购</h1>
            <p>KeGou</p>
          </div>
        </div>
        <div class="search">
          <input type="text" v-model="search" style="padding-left: 20px">
        </div>
        <div class="button">
          <input type="submit" value="搜索" @click="searchGoods">
        </div>
      </div>

      <div class="container" style="background-color: #fff;border-radius: 20px;margin-top: 70px;margin-left: 170px">
        <div class="row">

          <div class="title" style="font-size: 10px;" v-for="(product,index) in products" :key="index">
            <router-link :to="{name:'GoodsDetail',params:{id:product.id}}">
              <img :src="'data:image/jpeg;base64,'+product.pic" style="width: 130px;height: 150px">
              <div class="name">{{ product.name }}</div>
              <div class="money">$ <a>{{ product.price }}</a></div>
            </router-link>
          </div>

        </div>
      </div>
      <div v-show="show" class="add" style="text-align: center;margin-bottom: -20px"><a @click="GetProducts"
                                                                                        style="cursor: pointer;color: blue">加载更多</a>
      </div>
      <div class="footer">
        <p>反馈建议 | 官方空间 | 空间活动 | 空间应用 | ABCmouse腾讯版 | 腾讯原创馆 | QQ互联 | QQ登录 | 社交组件 | 应用侵权投诉 | Complaint Guidelines</p>
        <p>Copyright © 2005 - 2022 Tencent.All Rights Reserved.</p>
        <p><a href="https://beian.miit.gov.cn">湘ICP备2022018153号-1</a></p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [],
      search: '',
      show: true,
    }
  },
  methods: {
    reload() {
      location.reload()
    },
    //search product
    searchGoods() {
      if (this.search.length == 0) {//用户搜索关键字为空
        this.$notify.info({
          title: '系统信息',
          message: "请输入搜索的内容"
        })
      } else {
        this.$http.get("/product/searchProduct", {
          params: {
            search: this.search,
          },
          headers: {
            Authorization: "Bearer " + localStorage.getItem("front_token") //带着token去请求
          }
        }).then((rep) => {
          this.show = false
          this.products = rep.data.product
        }).catch((rep) => {
          console.log(rep)
        })
      }

    },
    GetInfo() {
      let username = this.$route.query.username
      let token = this.$route.query.token
      if (username != undefined || token != undefined) { //代表用户有信息
        this.$auth.setFrontAuth(this.$route.query.token, this.$route.query.username) // 设置token
      }
    },
    //获取商品列表 进入时最多九条
    GetProducts() {
      this.show = true
      let num = this.products.length
      this.$http.get("/product/GetProducts", {
        params: {num: num}
      }).then((rep) => {
        if (num != 0) {
          this.products = this.products.concat(rep.data.products)
        } else {
          this.products = rep.data.products
        }
        if (rep.data.code == 201) {
          alert(rep.data.msg)
        }
      }).catch((rep) => {
        console.log(rep)
      })
    },
    Logout() {
      this.$auth.delFrontAuth()
      this.$router.push("/login")
    }
  }
  ,
  mounted() {
    this.GetProducts();
    this.GetInfo();
  }
}

</script>
<style scoped>
* {
  padding: 0px;
  margin: 0px;
}

.docker {
  background-color: #EAE8EB;
}

.nav {
  border: 1px solid #F5F5F5;
  width: 100%;
  font-size: 10px;
  background-color: #F5F5F5;
}

.nav a {
  margin-top: 5px;
  margin-bottom: 5px;
  cursor: pointer;
}

.nav .div {
  margin-left: 30px;
  color: #8D8D8D;
}

.nav .push {
  margin-left: 30px;
  color: #8D8D8D;
}

.nav .push img {
  width: 15px;
  margin-left: 10px;
}

.logo {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  margin-left: 200px;
  margin-top: 40px;
  color: #EB5E2A;
}

.logo p {
  font-size: 20px;
  margin-top: -10px;
  margin-left: 9px;
}

.search input {
  border: 3px solid #EB5E2A;
  width: 650px;
  height: 45px;
  border-radius: 20px;
}

.search {
  margin-top: -75px;
  margin-left: 400px;
}

.button input {
  border: 1px solid #EB5E2A;
  width: 70px;
  background-color: #EB5E2A;
  color: #fff;
  font-weight: bolder;
  height: 36px;
  border-radius: 20px;
}

.button {
  margin-top: -40.5px;
  margin-left: 975px;
}

.container .row .title {
  border: 1px solid #fff;
  width: 32%;
  border-radius: 10px;
  background-color: #F7F9FA;
  height: 180px;
  margin-left: 11px;
  margin-top: 15px;
}

.container .row .title img {
  width: 150px;
  border-radius: 10px;
  margin-top: 12px;
  margin-left: 10px;
}

.container .row .title .name {
  margin-left: 175px;
  margin-top: -150px;
}

.container .row .title .money {
  margin-left: 180px;
  margin-top: 100px;
  color: #EB5E2A;
}

.container .row .title .money a {
  font-size: 24px;
}

.footer {
  margin-top: 20px;
  border: 1px solid #fff;
  text-align: center;
  font-size: 10px;
  background-color: #fff;
  padding-top: 10px;
  padding-bottom: 10px;
}

.footer p {
  margin-bottom: 0px;
}

</style>
